/*************************************************************************************************************
*                                                    UTOS
*                               A non-preemptive(cooperative) multitasking RTOS Kernel
*
* Copyright (c) 2013, HuangLiang (huangliang.tj@gmail.com)
* Under New BSD license, see "LICENSE" for detail
*
* @.file       utos_mem.c
* @.version    refer to macro "UT_VERSION" in utos.h
* @.brief      
* 
*************************************************************************************************************/
#include  "utos.h"

/*************************************************************************************************************
*                                             Global:  UT_MemCpy()
* @.brief     Copy a piece of memory from *psrc to *pdest
*
* @.argument  (1) void *pdest
*                 The start address of which you want to clear. As it is of type void*, you do not need to do
*                 explicit type cast.
*             (2) const void *psrc
*                 The souce address from which you need to copy. As it is of type void*, you do not need to do
*                 explicit type cast.
*             (3) UT_INT32U size
*                 The size of memory you want to copy, in Byte. It's your responsibility to provide the right
*                 value, or it will clear a space more or less than you want, which may cause critical damage.
*
* @.return    none.
*************************************************************************************************************/
void    UT_MemCpy(void *pdest, const void *psrc,UT_INT32U size)
{
    UT_INT8U  *pd, *ps;
    pd = (UT_INT8U  *)pdest;
    ps = (UT_INT8U  *)psrc;
    while(size)
    {
        *pd++ = *ps++;
        size--;
    }
}
/*************************************************************************************************************
*                                             Global:  UT_MemSet()
* @.brief     Set a piece of memory, set all data to 1. Be carefull of the address and boundary.
*
* @.argument  (1) void * pdest
*                 The start address of which you want to clear. As it is of type void*, you do not need to do
*                 explicit type cast.
*             (2) UT_INT32U size
*                 The size of memory you want to clear, in Byte. It's your responsibility to provide the right
*                 value, or it will clear a space more or less than you want, which may cause critical damage.
*
* @.return    none.
*************************************************************************************************************/
void    UT_MemSet(void * pdest, UT_INT32U size)
{
    UT_INT8U  *p = (UT_INT8U*)pdest;
    while(size)
    {
        *p++ = (UT_INT8U)0xff;
        size--;
    }
}
/*************************************************************************************************************
*                                             Global:  UT_MemClr()
* @.brief     Clear a piece of memory, set all data to 0. Be carefull of the address and boundary.
*
* @.argument  (1) void * pdest
*                 The start address of which you want to clear. As it is of type void*, you do not need to do
*                 explicit type cast.
*             (2) TX_INT32U size
*                 The size of memory you want to clear, in Byte. It's your responsibility to provide the right
*                 value, or it will clear a space more or less than you want, which may cause critical damage.
*
* @.return    none.
*************************************************************************************************************/
void    UT_MemClr(void  *pdest, UT_INT32U  size)
{
    UT_INT8U  *p = (UT_INT8U*)pdest;
    while(size)
    {
        *p++ = 0;
        size--;
    }
}

/*************************************************************************************************************
*                                             Global:  UT_MemCmp()
* @.brief     Compare two content of memory pointed by *pmem1 and *pmem2. If any Byte in each memory is 
*             different, it will return false;
*
* @.argument  (1) const void *pmem1
*                 Memory one.
*             (2) const void *pmem2
*                 Memory two.
*             (3) UT_INT32U size
*                 The size of memory you want to compare. You MUST make sure memory one and two are of the 
*                 same size.
*
* @.return    UT_FALSE  At least one byte in both memory are not the same.
*             UT_TRUE   Each memory has the same content
*************************************************************************************************************/
UT_BOOLEAN  UT_MemCmp(void const *pmem1,  void const *pmem2, UT_INT32U size)
{
    UT_INT8U  *p1, *p2;
    p1 = (UT_INT8U  *)pmem1;
    p2 = (UT_INT8U  *)pmem2;
    while(size)
    {
        if(*p1++ != *p2++)
            return UT_FALSE;
        size--;
    }
    return 1;
    
}
/*************************************************************************************************************
*                                                     END                                                    *
*************************************************************************************************************/
